﻿using PetkaWebDav.Server.Framework.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace PetkaWebDav.Server.Framework.BaseClasses
{
    public abstract class DavGetBase : DavMethodBase
    {
        /// <summary>
        /// Dav Resource Get Framework Base Class
        /// </summary>
        protected DavGetBase()
        {
            this.ValidateDavRequest += new DavRequestValidator(DavGetBase_ValidateDavRequest);
            this.InternalProcessDavRequest += new DavInternalProcessHandler(DavGetBase_InternalProcessDavRequest);

            this.ResponseCache = HttpCacheability.NoCache;
            this.ResponseCacheExpiration = DateTime.MinValue;
        }

        /// <summary>
        /// WebDav GET Response Codes
        /// </summary>
        public enum DavGetResponseCode : int
        {
            /// <summary>
            /// 0: None
            /// </summary>
            /// <remarks>
            ///		Default enumerator value
            /// </remarks>
            None = 0,

            /// <summary>
            /// 201: Created
            /// </summary>
            /// <remarks>
            ///		The resource was sent 
            /// </remarks>
            Created = 201
        }

        /// <summary>
        /// Output response content type
        /// </summary>
        protected string ContentType
        {
            get
            {
                return base.HttpApplication.Response.ContentType;
            }
            set
            {
                base.HttpApplication.Response.ContentType = value;
            }
        }

        /// <summary>
        /// Output response cacheability.
        /// </summary>
        /// <remarks>HttpCacheability.NoCache by default</remarks>
        protected HttpCacheability ResponseCache { get; set; }

        /// <summary>
        /// Output response cache expiration
        /// </summary>
        /// <remarks>No expiration by default</remarks>
        protected DateTime ResponseCacheExpiration { get; set; }

        /// <summary>
        /// Resource output response
        /// </summary>
        protected byte[] ResponseOutput { get; set; }

        private int DavGetBase_ValidateDavRequest(object sender, EventArgs e)
        {
            if (base.RequestLength != 0)
                return (int)ServerResponseCode.BadRequest;

            try
            {
                //Try to set the content type based on the requested extension
                this.ContentType = InternalFunctions.GetMimeType(this.RelativeRequestPath);
            }
            catch (Exception)
            {

                InternalFunctions.WriteDebugLog("erorr getting mime for : " + this.RelativeRequestPath);
            }

            return (int)ServerResponseCode.Ok;
        }

        private int DavGetBase_InternalProcessDavRequest(object sender, EventArgs e)
        {
            base.HttpApplication.Response.Cache.SetCacheability(ResponseCache);

            if (ResponseCacheExpiration != DateTime.MinValue)
                this.HttpApplication.Response.Cache.SetExpires(ResponseCacheExpiration);

            if (this.ResponseOutput.Length > 0)
            {
                //Write the response
                using (BinaryWriter _outputStream = new BinaryWriter(base.HttpApplication.Response.OutputStream))
                {
                    _outputStream.Write(this.ResponseOutput);
                    _outputStream.Close();
                }
            }

            return (int)ServerResponseCode.Ok;
            //DavGetResponseCode.Created;
        }
    }
}
